AppUtil = {
	retriveInfo: function(appId) {
		if (appId == null) {
			return Vari.app;
		}
		
		var rt = null;				
		$.ajax({
			type : 'GET',
			dataType : 'json', contentType: "application/json",
			async : false,
			url : Vari.ApiRestV1 + 'apps/' + appId,

			success : function(json) {
				rt = AppUtil.genAppVo(json.app);								
			} 
		});				
		return rt;
	},
	createSession: function(appId) {
		var cometd = $.cometd;

    	function _connectionEstablished()
    	{
    		$('#cometdMsg').append('<div>CometD Connection Established</div>');
    	}

    	function _connectionBroken()
    	{
    		$('#cometdMsg').append('<div>CometD Connection Broken</div>');
    	}

    	function _connectionClosed()
    	{
    		$('#cometdMsg').append('<div>CometD Connection Closed</div>');
    	}

    	// Function that manages the connection status with the Bayeux server
    	var _connected = false;
    	function _metaConnect(message)
    	{
    		if (cometd.isDisconnected())
    		{
    			_connected = false;
    			_connectionClosed();
    			return;
    		}

    		var wasConnected = _connected;
    		_connected = message.successful === true;
    		if (!wasConnected && _connected)
    		{
    			_connectionEstablished();
    		}
    		else if (wasConnected && !_connected)
    		{
    			_connectionBroken();
    		} 
    	}

    	// Function invoked when first contacting the server and
    	// when the server has lost the state of this client
    	function _metaHandshake(handshake)
    	{
    		if (handshake.successful === true)
    		{
    			cometd.batch(function()
    			{
//    				cometd.publish('/service/session', 
//    						{sessionId: appId, msg: 'Thank you for coming!' });
    			});
    		}
    	}

    	// Disconnect when the page unloads
    	$(window).unload(function()
    	{
    		cometd.disconnect(true);
    	});

    	var cometURL = Custant.WebRoot + "cometd";
    	cometd.configure({ 
    		url: cometURL,
    		//logLevel: 'debug' 
    	});

    	cometd.addListener('/meta/handshake', _metaHandshake);
    	cometd.addListener('/meta/connect', _metaConnect);

    	cometd.handshake();
	},
	conectToSession: function(appId, onMsg) {
	   	var cometd = $.cometd;

    	function _connectionEstablished() {
    		$('#cometdMsg').append('<div>CometD Connection Established</div>');
    	}

    	function _connectionBroken() {
    		$('#cometdMsg').append('<div>CometD Connection Broken</div>');
    	}

    	function _connectionClosed() {
    		$('#cometdMsg').append('<div>CometD Connection Closed</div>');
    	}

    	// Function that manages the connection status with the Bayeux server
    	var _connected = false;
    	function _metaConnect(message) {
    		if (cometd.isDisconnected())
    		{
    			_connected = false;
    			_connectionClosed();
    			return;
    		}

    		var wasConnected = _connected;
    		_connected = message.successful === true;
    		if (!wasConnected && _connected)
    		{
    			_connectionEstablished();
    		}
    		else if (wasConnected && !_connected)
    		{
    			_connectionBroken();
    		} 
    	}

    	// Function invoked when first contacting the server and
    	// when the server has lost the state of this client
    	function _metaHandshake(handshake) {
    		if (handshake.successful === true) {
    			cometd.batch(function() {
    				cometd.subscribe('/channel/' + appId, function(message)
    				{
    					$('#cometdMsg').append('<div>Server Push form: ' + message.data.formId + '</div>');   
    					console.log(message);
    					onMsg(message);
    				});
    			});
    		}
    	}

    	// Disconnect when the page unloads
    	$(window).unload(function() {
    		cometd.disconnect(true);
    	});

    	var cometURL = Custant.WebRoot + "cometd";
    	cometd.configure({ 
    		url: cometURL,
    		//logLevel: 'debug'
    	});

    	cometd.addListener('/meta/handshake', _metaHandshake);
    	cometd.addListener('/meta/connect', _metaConnect);

    	cometd.handshake();
	},
	genAppVo: function (appTo, i){		
		var appVo = App.AppVo.create();
		
		Ember.mixin(appVo, {
			id: appTo.id,
			name: appTo.name,
			runQrCode: appTo.runQrCode,
			descr: appTo.descr,
			
			num: i
		});
		return appVo;
	},
};